<?php
class UsersSoctype extends AppModel {

	var $name = 'UsersSoctype';
	var $useDbConfig = 'users';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
/**
			'User' => array('className' => 'User',
								'foreignKey' => 'user_id',
								'conditions' => '',
								'fields' => array('id', 'login'),
								'order' => ''
			),
*/
			'Soctype' => array('className' => 'Soctype',
								'foreignKey' => 'soctype_id',
								'conditions' => '',
								'fields' => '',
								'order' => ''
			),
	);


	function getUsersList($types = array()) {
		$this->unBindModel(array('belongsTo'=>array('Soctype')));
		$this->recursive = 3;
		$conditions = array('UsersSoctype.soctype_id'=>$types);
		return $this->find('all', array('conditions'=>$conditions));
	}

	function get($uid = null) {
		$this->unBindModel(array('belongsTo'=>array('User')));
		$c = array('user_id'=>$uid);
		return $this->find('first', array('conditions'=>$c));
	}

	function getSoctype($uid) {
		$c = array('user_id'=>$uid);
		return $this->find('first', array('conditions'=>$c, 'recursive'=>-1));	
	}

	function saveType($uid = null, $typeid = null) {
		$data = false;
		if (intval($typeid) < 16 && $uid) {
			if ($id = $this->field('id', array('user_id'=>$uid))) {
				$dataS['id'] = $id;
			}
			$dataS['user_id'] = $uid;
			$dataS['soctype_id'] = $typeid;
			$data = $this->save($dataS);
		}
		return $data;
	}

	function changeStatus($uid, $status) {
		$c = array('user_id'=>$uid);
		$id = $this->field('id', $c);

		if ($id) {
			$this->id = $id;
			$this->saveField('status', $status);
		}
	}

}
?>
